#!/usr/bin/env python
from config import *

import sys
import xmlrpclib
import wave
from array import array
from struct import pack
import random

FORMAT = 'LIN16'
LSB = True

class Recognizer:
    def start(self, url):
        self.summit = xmlrpclib.ServerProxy(url)
        self.sid = self.summit.session.open("", FORMAT, LSB, DOWNSAMPLE_RATE, "julia", "test")
        self.summit.session.utterance.open(self.sid)
    
    # Chunk should be a packed integer array of CHUNK_SIZE
    def send_chunk(self, chunk):
        n = self.summit.session.utterance.write(self.sid, xmlrpclib.Binary(chunk))

    # Close the session with the server and get results back
    def get_results(self):
        nbest = self.summit.session.utterance.close(self.sid)
        results = []
        for h in nbest:
            results.append((h['score'], h['text']))
        if results == []:
            results = [(0, "")]
        return results
